package com.juntuo.filter;

import com.juntuo.entity.Token;
import com.juntuo.entity.User;
import com.juntuo.service.TokenService;
import com.juntuo.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ApiTestFilter implements Filter {
    private UserService userService;
    private TokenService tokenService;
    @Override
    public void destroy() {

    }



    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext sc = filterConfig.getServletContext();
        XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);

        if(cxt != null && cxt.getBean(UserService.class) != null && userService == null) {
            userService =  cxt.getBean(UserService.class);
        }
        if(cxt != null && cxt.getBean(TokenService.class) != null && tokenService == null) {
            tokenService = cxt.getBean(TokenService.class);
        }
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //签名验证
//        Map<String,String> params = ApiSignUtil.changeToMap(request);
//        try{
//            if (!ApiSignUtil.verify(params)){
//                response.sendRedirect("/app/filter/sign");
//                return;
//            }
//        }catch (Exception e){
//            response.sendRedirect("/app/filter/sign");
//            return;
//        }
        //判断token是否为空
        String token = request.getParameter("token");
        if(StringUtils.isEmpty(token)){
            response.sendRedirect("/api/test/noAuth");
            return;
        }
//        //判断token是否过期
//        Date sevenDaysAgo = DateTimeUtil.getSevenDaysAgo();
//        BuguDao<UserToken> userTokenDao = DaoCache.getInstance().get(UserToken.class);
        //判断token是否存在
        Token userToken = new Token();
        userToken.setToken(token);
        userToken=tokenService.selectOne(userToken);
        if(userToken == null){
            response.sendRedirect("/api/test/noAuth");
            return;
        }
        Map<String, Object> extraParams = new HashMap<String, Object>();
        extraParams.put("userId",userToken.getUserId());
        //利用原始的request对象创建自己扩展的request对象并添加自定义参数
        RequestParameterWrapper requestParameterWrapper = new RequestParameterWrapper(request);
        requestParameterWrapper.addParameters(extraParams);

        chain.doFilter(requestParameterWrapper, resp);
    }
}
